﻿using System;
using System.Collections.Generic;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.IoC;
using PhoneCore.Framework.Storage;

namespace SecureBox.UI.Infrastructure.Plugins
{
    /// <summary>
    /// Registers core services, should be executed first
    /// </summary>
    public class InitPlugin: BootstrapperPlugin
    {
        public InitPlugin(IConfigSection config) : base(config) { }

        public override bool Run()
        {
            try
            {
                //init settings
                var settingsService = Container.Resolve<ISettingService>();
                if(!settingsService.IsExist(Constants.TraceLevelSetting))
                    settingsService.Save(Constants.TraceLevelSetting, Trace.Level);
                int level = settingsService.Load<int>(Constants.TraceLevelSetting);
                Trace.Info(Category, String.Format("set trace level to user defined: {0}", level));
                Trace.Level = level;
                return true;
            }
            catch (Exception ex)
            {
                Trace.Fatal(Category, "registration failed", ex);
                throw;
            }
        }

        public override bool Update()
        {
            //not updatable
            Trace.Info(Category, "update");
            return false;
        }
    }
}
